package com.gao.f_reactor.d.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;

/**
 * @author gao
 * @time 2020/08/23 17:56:20
 */
public class NioServer {

    private int port;
    private ServerSocketChannel serverSocketChannel;
    private Reactor reactor;

    public NioServer(int port) throws IOException {
        this.port = port;
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(port));
        serverSocketChannel.configureBlocking(false);
        reactor = new Reactor(serverSocketChannel);
    }

    public void start() {
        new Thread(reactor).start();
    }

    public static void main(String[] args) throws IOException {
        int port = 8888;
        if (args.length > 0) {
            try {
                port = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        NioServer server = new NioServer(port);
        server.start();
    }
}
